package com.nw.serviceImpl;

import com.nw.dao.CategoryDao;
import com.nw.entity.Category;
import com.nw.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Resource
    CategoryDao categoryDao;
    @Override
    public HashMap<String, Object> addCategoryLevel1(Category category) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            category.setCategoryId(UUID.randomUUID().toString());
            category.setCategoryLevel("1");
            category.setParentId(UUID.randomUUID().toString());
            categoryDao.addCategoryLevel1(category);
            map.put("code","0");
            map.put("msg","类别添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","0");
            map.put("msg","类别添加失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> deleteCategoryLevel1(Category category) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            categoryDao.deleteCategoryLevel1(category);
            map.put("code","0");
            map.put("msg","类别删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","0");
            map.put("msg","类别删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> updateCategoryLevel1(Category category) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            categoryDao.updateCategoryLevel1(category);
            map.put("code","0");
            map.put("msg","类别修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","0");
            map.put("msg","类别修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> queryCategoryLevel1() {
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<Category> categories = categoryDao.queryCategoryLevel1();
            map.put("code","0");
            map.put("msg","类别查询成功");
            map.put("data",categories);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","0");
            map.put("msg","类别查询失败");
        }
        return map;
    }
}
